﻿#region Copyright (C) Sector7 Software Studio. All rights reserved
/*******************************************************************************************
 * Creation
 *      Author:         Wolf Lai
 *      Date:           2012/1/3 17:43:36
 *      Version:        
 * Modification 
 *      Author: 
 *      Date: 
 *      Description: 
 *      Version:
 *******************************************************************************************/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sector7.Restaurant.EntityType;
using System.Data;
using Sector7.Framework.Core;
using System.Data.SqlClient;
using Sector7.Framework.Utility;
using Sector7.Restaurant.DAO.Utility;
using Sector7.Framework.DB;

namespace Sector7.Restaurant.DAO.OleDB
{
    /// <summary>
    /// 员工组数据访问对象
    /// </summary>
    public class EmployeeGroupDAO : IEmployeeGroupDAO
    {
        #region Public functions
        public int Create(RS_EmployeeGroup group)
        {
            using (DBHelper opt = new DBHelper())
            {
                string sql =
                    "INSERT INTO RS_EmployeeGroup   " +
                    "       (GroupName              " +
                    "       ,BonusRate              " +
                    "       ,Permission)            " +
                    "   VALUES                      " +
                    "       (@GroupName             " +
                    "       ,@BonusRate             " +
                    "       ,@Permission)           " +
                    "SELECT @@IDENTITY              ";

                return Convert.ToInt32(opt.ExecuteScalar(
                    sql,
                    DBUtil.CreateSqlParameter(group.GroupName, "GroupName"),
                    DBUtil.CreateSqlParameter(group.BonusRate, "BonusRate"),
                    DBUtil.CreateSqlParameter(group.Permission, "Permission")));
            }
        }

        public RS_EmployeeGroup Read(int groupId)
        {
            using (DBHelper opt = new DBHelper())
            {
                object group = null;
                string sql =
                    "SELECT *                       " +
                    "   FROM RS_EmployeeGroup       " +
                    "   WHERE GroupId = @GroupId    ";

                SqlDataReader reader = opt.ExecuteQuery(
                    sql,
                    DBUtil.CreateSqlParameter(groupId, "GroupId"));

                if (reader.Read())
                {
                    group = new RS_EmployeeGroup();

                    DBUtil.ReadData(reader, ref group);
                }

                return group as RS_EmployeeGroup;
            }
        }

        public List<RS_EmployeeGroup> ReadAll(bool isReadEmployee)
        {
            using (DBHelper opt = new DBHelper())
            {
                List<RS_EmployeeGroup> groups = new List<RS_EmployeeGroup>();
                string sql =
                    "SELECT *                   " +
                    "   FROM RS_EmployeeGroup   ";

                SqlDataReader reader = opt.ExecuteQuery(sql);

                while (reader.Read())
                {
                    object group = new RS_EmployeeGroup();

                    DBUtil.ReadData(reader, ref group);

                    if (isReadEmployee)
                    {
                        IEmployeeDAO employeeDAO = new EmployeeDAO();
                        (group as RS_EmployeeGroup).Employees = employeeDAO.ReadAll((group as RS_EmployeeGroup).GroupId);
                    }

                    groups.Add(group as RS_EmployeeGroup);
                }

                return groups;
            }
        }

        public int Update(EntityDelta delta)
        {
            using (DBHelper opt = new DBHelper())
            {
                return opt.ExecuteNonQuery(
                    DBUtil.CreateUpdateSql(delta),
                    DBUtil.CreateUpdateParameters(delta));
            }
        }

        public DBOptResultType Delete(int groupId)
        {
            using (DBHelper opt = new DBHelper())
            {
                string sql =
                    "DELETE FROM RS_EmployeeGroup   " +
                    "   WHERE GroupId = @GroupId    ";

                return (DBOptResultType)opt.ExecuteNonQuery(
                     sql,
                     DBUtil.CreateSqlParameter(groupId, "GroupId"));
            }
        }
        #endregion
    }
}
